package listbyorder.access201_300.test221;

import java.util.Map;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/8/21 14:11
 */
public class Solution1 {

    // 暴力
    public int maximalSquare(char[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
        int res = 0;
        int m = matrix.length, n = matrix[0].length;
        int maxL = Math.min(m, n);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == '1' && Math.min(n - j, m - i) > res) {
                    int r = i + 1, c = j + 1, l = 1;
                    // 寻找当前最大的边长
                    while (r < m && c < n) {
                        if (matrix[r][c] == '0') break;
                        boolean f = false;
                        for (int k = 0; k < r - i; k++) {
                            if (matrix[r][j + k] == '0') {
                                f = true;
                                break;
                            }
                            if (matrix[i + k][c] == '0') {
                                f = true;
                                break;
                            }
                        }
                        if (f) break;
                        l++;
                        r++;
                        c++;
                    }
                    res = Math.max(res, l);
                }
            }
        }
        return res * res;
    }
}
